﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLTB.Data;
using QLTB.Entities;
using _4rumDCT.App_Code;

namespace QuanLyThietBi.DCTEDU_.BaoCao
{
    public partial class DanhSachPhanMem : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCatalog.BindPhongHoc(cboPhongHoc);
                if (Request.Cookies["DangNhap"] != null)
                {
                    HttpCookie cookie = Request.Cookies.Get("UserNameAndPassword");
                    if (cookie != null)
                    {
                        CanBo ent_CanBo = DataRepository.CanBoProvider.GetByTenTruyCap(cookie.Values["UserName"]);
                        if (ent_CanBo.MaQuyen == 2)
                        {
                            cmdTaoPhanMem.Visible = true;
                            BindPhongHoc(ent_CanBo.MaCanBo);
                        }
                        else
                        {
                            cmdTaoPhanMem.Visible = false;
                        }
                    }
                }

            }
        }
        protected void rpPhongHoc_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                H_PhongHocId.Value = id.ToString();

                litPhong.Text = ": " + DataRepository.PhongHocProvider.GetByMaPhong(id).TenPhong;
                BindDSPhanMem();
                
            }
        }

        private void BindPhongHoc(string maCanBo)
        {
            int totalRow = 0;
            string whereClause = "MaCanBo =" + maCanBo;
            TList<PhongHoc> TList_PhongHoc = DataRepository.PhongHocProvider.GetPaged(whereClause, "", 0, 0,
                out totalRow);
            rpPhongHoc.DataSource = TList_PhongHoc;
            rpPhongHoc.DataBind();
        }

        protected void rpPhanMem_OnItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                ImageButton btn = (ImageButton)e.CommandSource;
                if (btn.ImageUrl == "~/Images/unchecked_checkbox.png")
                {
                    btn.ImageUrl = "~/Images/checked_checkbox.png";
                }
                else
                {
                    btn.ImageUrl = "~/Images/unchecked_checkbox.png";
                }
            }
        }

        protected void cmdLuu_OnClick(object sender, EventArgs e)
        {
            bool status = false;
            for (int i = 0; i < rpDSPhanMem.Items.Count; i++)
            {
                string maPhanMem = ((HiddenField)rpDSPhanMem.Items[i].FindControl("H_MaPhanMem")).Value;
                ImageButton cmdSelect = ((ImageButton)rpDSPhanMem.Items[i].FindControl("cmdSelect"));
                PhongHocPhanMem ent_PHPM = new PhongHocPhanMem();
                ent_PHPM.MaPhong = Convert.ToInt32(H_PhongHocId.Value);
                ent_PHPM.MaPhanMem = Convert.ToInt32(maPhanMem);
                if (cmdSelect.ImageUrl == "~/Images/checked_checkbox.png")
                {

                    if (getAnh(maPhanMem) == true)
                    {
                        ent_PHPM.MaPhpm = getMaPhongHocPhanMem(maPhanMem);
                        status = DataRepository.PhongHocPhanMemProvider.Update(ent_PHPM);
                    }
                    else
                    {
                        status = DataRepository.PhongHocPhanMemProvider.Insert(ent_PHPM);
                    }
                }
                else
                {
                    if (getAnh(maPhanMem) == true)
                    {
                        status = DataRepository.PhongHocPhanMemProvider.Delete(getMaPhongHocPhanMem(maPhanMem));
                    }
                }
               
            }
            if (!status)
            {
                ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI, Constant.CAPNHAT_THATBAI, true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THANHCONG, Constant.CAPNHAT_THANHCONG, true);
            }
        }

        public string getTenCanBo(object ob)
        {
            string id = ob.ToString();
            return DataRepository.CanBoProvider.GetByMaCanBo(id).HoTen;

        }

        public bool getAnh(object pm)
        {
            string maPM = pm.ToString();
            string maPH = H_PhongHocId.Value;
            int totalRow = 0;
            string whereClause = "MaPhanMem = " + maPM + " and MaPhong=" + maPH;
            TList<PhongHocPhanMem> TList_PHPM = DataRepository.PhongHocPhanMemProvider.GetPaged(whereClause, "", 0, 0,
                out totalRow);
            if (TList_PHPM.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int getMaPhongHocPhanMem(object pm)
        {
            string maPM = pm.ToString();
            string maPH = H_PhongHocId.Value;
            int totalRow = 0;
            string whereClause = "MaPhanMem = " + maPM + " and MaPhong=" + maPH;
            TList<PhongHocPhanMem> TList_PHPM = DataRepository.PhongHocPhanMemProvider.GetPaged(whereClause, "", 0, 0,
                out totalRow);
            if (TList_PHPM.Count > 0)
            {
                return TList_PHPM[0].MaPhpm;
            }
            else
            {
                return 0;
            }
        }

        private void BindDSPhanMem()
        {
            TList<PhanMem> TList_PhanMem = DataRepository.PhanMemProvider.GetAll();
            rpDSPhanMem.DataSource = TList_PhanMem;
            rpDSPhanMem.DataBind();
        }

    
        protected void cmdSave_Click(object sender, EventArgs e)
        {
            bool updateStatus = false;
            try
            {
                PhanMem entPhanMem = new PhanMem();
                entPhanMem.MaPhanMem = Convert.ToInt32(H_CatalogId.Value);
                entPhanMem.TenPhanMem = txtTen.Text;
                entPhanMem.DungLuong = txtDungLuong.Text;
                entPhanMem.DuongDan = txtDuongDan.Text;
                entPhanMem.GhiChu = txtGhiChu.Text;
                if (H_CatalogId.Value == "0")
                {
                    updateStatus = DataRepository.PhanMemProvider.Insert(entPhanMem);
                }
                else
                {
                    entPhanMem.EntityState = EntityState.Changed;
                    updateStatus = DataRepository.PhanMemProvider.Update(entPhanMem);
                }
                if (updateStatus)
                {
                    ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THANHCONG, Constant.CAPNHAT_THANHCONG, true);
                    cmdThemPhanMem.Visible = true;
                    cmdLuu.Visible = true;
                    pnThemPhanMem.Visible = false;
                    BindDSPhanMem();
                    cmdReset_Click(sender, e);
                }
                else
                    ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI, Constant.CAPNHAT_THATBAI, true);
            }
            catch (Exception ex)
            {
                Common.WriteLog("DanhSachPhanMem.aspx.cs", "cmdSave_Click", ex.ToString());
                ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI, Constant.CAPNHAT_THATBAI, true);
            }
        }

        protected void cmdReset_Click(object sender, EventArgs e)
        {
   
            txtTen.Text = "";
            txtDungLuong.Text = "";
            txtDuongDan.Text = "";
            H_CatalogId.Value = "0";
            txtGhiChu.Text = "";

        }

        protected void cboPhongHoc_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            pnDSPhanMem.Visible = true;
            pnPhanMem.Visible = false;
            litPhong.Text = "của " + "<font color=red>Phòng máy tính " + cboPhongHoc.SelectedValue + "</font>(" + DataRepository.ThietBiProvider.Find("MaPhong=" + cboPhongHoc.SelectedValue).Count + " máy)";
            BindPhanMem();
        }

        private void BindPhanMem()
        {
            int totalRow = 0;
            string whereClause = "MaPhanMem in (Select MaPhanMem from PhongHocPhanMem where MaPhong=" + cboPhongHoc.SelectedValue + ")";
            TList<PhanMem> TList_PhanMem = DataRepository.PhanMemProvider.GetPaged(whereClause, "", 0, 0, out totalRow);
            rpPhanMem.DataSource = TList_PhanMem;
            rpPhanMem.DataBind();
        }

        protected void cmdTaoPhanMem_OnClick(object sender, EventArgs e)
        {
            litPhong.Text = "";
            cboPhongHoc.SelectedValue = "0";
            pnDSPhanMem.Visible = false;
            pnPhanMem.Visible = true;
            cmdLuu.Visible = true;
            cmdThemPhanMem.Visible = true;
        }

        protected void cmdThemPhanMem_OnClick(object sender, EventArgs e)
        {
            pnThemPhanMem.Visible = true;
            cmdLuu.Visible = false;
            cmdThemPhanMem.Visible = false;
        }
    }
}